﻿--[[
   @brief    GroupBannerList
   @author   vincentlao
   @version  1.0
   @date     2015-06-11
--]]

ierd_tgp.tpf_ui.Log("loading banner_list.lua");

ierd_tgp.tpf_ui.Log("Require model.lua");
ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");

ierd_tgp.tpf_ui.Log("Require group_banner.lua");
ierd_tgp.tpf_ui.Require("social\\group_list\\group_banner.lua");

ierd_tgp.tpf_ui.Require("social\\talk_dialog\\control.lua");

GroupBannerList = {
    list_ctr_ = nil;
    uilib_ = nil;
    in_refreshing = false;
    group2banner_ = {};
    ctrl2banner_ = {};
    lasttime_ = os.time();
    message_not_belong_here_ = {};
};

function GroupBannerList:log(msg, core_msg)
    base_utility.log(string.format("[GroupBannerList] %s", msg), core_msg);
end

function GroupBannerList:attach(uilib, ctrl_id)
    self:log(string.format("attach %d", ctrl_id));
    self.uilib_ = uilib;
    self.list_ctr_ = MainCommon.get_ctrl_ptr(uilib, ctrl_id, "Tenio::IDraggableList");
    GroupBannerListEventListener.add_tick();
    self.message_not_belong_here_ = {};
end

function GroupBannerList:detach()
    self:log("detach");
    self.group2banner_ = {};
    self.ctrl2banner_ = {};
    self.list_ctr_ = nil;
    GroupBannerListEventListener.remove_tick();
end

function GroupBannerList:show(show)
    if self.list_ctr_ ~= nil then
        self.list_ctr_:Show(show);
    end    
end

function GroupBannerList:show_group_list()
    self:remove_all_group();
    group_list_model.sort();
    local groups = group_list_model.get_group_list();
    if groups == nil then
        return;
    end

    for i = 1, #groups do
        self:insert_group(i - 1, groups[i]);
    end
end

function GroupBannerList:insert_group(pos, group_info)
    self:log(string.format("insert_group"));
    if self.list_ctr_ == nil then
        self:log(string.format("insert_group, list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end

    if self.in_refreshing then
        self:log(string.format("[insert_group]return directtly while in refreshing"));
        return;
    end

    local const_ui_str, str_hoder = U8toCUIStr("social\\group_banner.tpf");
    local item_id = self.list_ctr_:InsertItem(pos, const_ui_str);
    self.list_ctr_:SetItemDraggable(pos, false);

    local banner = GroupBanner:new(group_info.group_id, item_id);
    self.ctrl2banner_[item_id] = banner;
    self.group2banner_[group_info.group_id] = banner;
end

function GroupBannerList:remove_group(group_id)
    self:log(string.format("remove_group"));
    if self.list_ctr_ == nil then
        self:log(string.format("remove_group, list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end

    if self.in_refreshing then
        self:log(string.format("[remove_group]return directtly while in refreshing"));
        return;
    end

    local banner = GroupBannerList:get_banner(group_id);
    if banner == nil then
        return;
    end

    local item_id = banner:get_root_id();
    local item_index = self.list_ctr_:GetIdxByItemRootID(item_id);
    self.list_ctr_:RemoveItem(item_index);

    self.group2banner_[group_id] = nil;
    self.ctrl2banner_[item_id] = nil;

    group_list_model.remove_group_info_by_id(group_id);
    group_message_model.clear_group_messages(group_id);

    self:try_clear_red_point();

    local param = {};
    param["group_id"] = group_id;
    event_center.send_event(group_event.REMOVE_GROUP_FROM_GROUP_LIST,base_utility.table_to_json(param));
end

function GroupBannerList:remove_all_group()
    self:log(string.format("remove_all_group"));
    if self.list_ctr_ == nil then
        self:log(string.format("remove_all_group, list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end

    if self.in_refreshing then
        self:log(string.format("[remove_all_group]return directtly while in refreshing"));
        return;
    end

    while self.list_ctr_:GetItemCount() > 0 do
        self.list_ctr_:RemoveItem(0);
    end

    self.group2banner_ = {};
    self.ctrl2banner_ = {};

    group_message_model.clear_all_group_messages();

    self:try_clear_red_point();
end

function GroupBannerList:re_sort_group()
    if self.list_ctr_ == nil then
        self:log(string.format("remove_all_group, list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end

    group_list_model.sort();
    local groups = group_list_model.get_group_list();
    if groups == nil then
        return;
    end

    self.group2banner_ = {};

    for i = 1, #groups do
        local item_id = self.list_ctr_:GetItemRootIDByIdx(i - 1);
        if item_id == nil or item_id == 0 then
            self:log(string.format("insert_group, GetItemRootIDByIdx failed"));
            break;
        end

        local banner = self.ctrl2banner_[item_id];
        if banner == nil then
            self:log(string.format("insert_group, get banner failed"));
            break;
        end

        banner.group_id_ = groups[i].group_id;
        self.ctrl2banner_[item_id] = banner;
        self.group2banner_[groups[i].group_id] = banner;
        banner:update(groups[i].group_id);
    end
end

function GroupBannerList:on_get_group_unreaed_message_count(unread_param)
    if unread_param == nil then
        return;
    end

    local show_red_poind = false;

    for group_i = 1, #unread_param do
        --group_message_model.clear_group_messages(unread_param[group_i].id);
        local need_seed_clear_message_event = false;
        local old_count = group_list_model.get_group_history_unread_msg_count(unread_param[group_i].id);
        if old_count ~= unread_param[group_i].count then
            need_seed_clear_message_event = true;
        end
        --if group_list_model.get_group_history_unread_msg_count_remotely(unread_param[group_i].id) == 0 then
            group_list_model.set_history_unread_msg_count(unread_param[group_i].id, unread_param[group_i].count);
            group_list_model.set_history_unread_msg_count_remotely(unread_param[group_i].id, unread_param[group_i].count);
        --end

        local unread_message_event = {};
        unread_message_event.group_id = unread_msg[group_i].id;  
        if group_list_model.get_group_history_unread_msg_count(unread_param[group_i].id) > 0 then
            event_center.send_event(group_event.GROUP_HAVE_UNREAD_MESSAGE,base_utility.table_to_json(unread_message_event));        
        else
            if need_seed_clear_message_event then
                event_center.send_event(group_event.GROUP_CLEAR_MESSAGE,base_utility.table_to_json(unread_message_event));
            end
        end

        if group_list_model.get_group_history_unread_msg_count(unread_param[group_i].id) > 0 then
            show_red_poind = true;
        end
    end

    --group_message_model.clear_all_group_messages();

    self:re_sort_group();

    --[[local show_red_point = {};
    show_red_point.show = "true";  

    if show_red_poind == true then
        show_red_point.show = "true";  
    else
        show_red_point.show = "false";  
    end

    event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT,base_utility.table_to_json(show_red_point));  ]]--
    local count_friend = friend_list_model.get_total_unread_msg_count();
    local count_group = group_list_model.get_total_unread_msg_count();
    local total = count_friend + count_group;

    local show_red_point = {};
    if total == 0 then
        show_red_point.show = "false";
    else
        show_red_point.show = "true";
    end
    event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT, base_utility.table_to_json(show_red_point));
end

function GroupBannerList:on_group_new_message_come(msg)  
    if msg == nil or msg.group_id == nil then
        return;
    end

    local group_info = group_list_model.get_group_info(msg.group_id);
    if group_info == nil then
        self.message_not_belong_here_[msg.group_id] = msg;
    else
        self:update_last_msg(msg);
        local is_session_opened = talk_dialog_control.is_group_session_active(msg.group_id);
        if is_session_opened == false then
            self:re_sort_group();
        end
    end  
end

function GroupBannerList:update_last_msg(msg)
    if msg.msg_type == 1 then
        local group_info = group_list_model.get_group_info(msg.group_id);
        if group_info.group_type ~= "MatchLiveRoom"  and
           group_info.group_type ~= "CommonTopicRoom" then
            group_list_model.set_last_msg(msg.group_id, msg);
            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social_network:set_group_last_msg(base_utility.table_to_json(msg));  
        else
            -- 直播消息群 群资料更改 不需要进最后一条消息中
        end
    else
        group_list_model.set_last_msg(msg.group_id, msg);
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:set_group_last_msg(base_utility.table_to_json(msg));  
    end

end

function GroupBannerList:deal_with_newly_group_with_message_not_belong_here(group_id)  
    if group_list_model.get_group_info(group_id) == nil then
        return;
    end

    if self.message_not_belong_here_[group_id] == nil then
        return;
    end

    if group_list_model.get_last_msg(group_id) ~= nil then
        return;
    end

    self:update_last_msg(self.message_not_belong_here_[group_id]);

    self.message_not_belong_here_[group_id] = nil;
    self:re_sort_group();
end

function GroupBannerList:deal_with_delete_group_with_message_not_belong_here(group_id)  
    self.message_not_belong_here_[group_id] = nil;
end

function GroupBannerList:on_send_msg_to_group(msg)
    if msg == nil or msg.group_id == nil then
        return;
    end

    group_list_model.set_last_msg(msg.group_id, msg);
    self:re_sort_group();    

    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:set_group_last_msg(base_utility.table_to_json(msg));  
end;

function GroupBannerList:on_group_clear_message(group_id)
    --group_list_model.clear_history_unread_msg_count(group_id);
    self:re_sort_group();
    self:try_clear_red_point();
end

function GroupBannerList:try_clear_red_point()
    --[[local show_red_poind = false;

    for k,v in pairs(group_message_model.group_message_) do
        if #v > 0 then
            show_red_poind = true;
        end
    end

    for index = 1, #group_list_model.group_table_ do
        if group_list_model.get_group_history_unread_msg_count(group_list_model.group_table_[index].group_id) > 0 then
            show_red_poind = true;
        end
    end

    local show_red_point = {};
    show_red_point.show = "true";  

    if show_red_poind == true then -- 我们是来清理红点的，不是来增加红点的。
        show_red_point.show = "true";  
    else
        show_red_point.show = "false";
        event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT,base_utility.table_to_json(show_red_point));
    end]]--
    local count_friend = friend_list_model.get_total_unread_msg_count();
    local count_group = group_list_model.get_total_unread_msg_count();
    local total = count_friend + count_group;
    local show_red_point = {};
    if total == 0 then
        show_red_point.show = "false";
    else
        show_red_point.show = "true";
    end
    event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT, base_utility.table_to_json(show_red_point)); 
end

function GroupBannerList:on_group_mask_update(group_id)
    local banner = GroupBannerList:get_banner(group_id);
    if banner == nil then
        return;
    end

    banner:update_mask_info(); 
end

function GroupBannerList:check_group_by_banner_id(banner_id)
    for key, value in pairs(self.ctrl2banner_) do
        if key == banner_id then
            value:set_selected(true);
            value:set_selected_state();
        else
            value:set_selected(false);
            value:set_normal_state();
        end
    end
end

function GroupBannerList:re_arrange_item_pos()
    self:log(string.format("re_arrange_item_pos"));
    if self.list_ctr_ == nil then
        self:log(string.format("re_arrange_item_pos, list_ctr_ is NIL, may be not attached yet"),
                            true);
        return;
    end
    
    if self.in_refreshing then
        self:log(string.format("[re_arrange_item_pos]return directtly while in refreshing"));
        return;
    end
    
    self.list_ctr_:ReArrangeItemPos();
end

function GroupBannerList:refresh_list()
    if self.list_ctr_ == nil then
        self:log(string.format("refresh_list, list_ctr_ is NIL, may be not attached yet"));
        return;
    end
    self.group2banner_ = {};
    self.ctrl2banner_ = {};

    -- 设置列表控件的Banner数量
    --self.list_ctr_:SetItemCount(count);
    -- 遍历game_list， 逐一设置数据

    self.in_refreshing = false;
    self:re_arrange_item_pos();
end

function GroupBannerList:get_banner(group_id)
    self:log(string.format("get_banner, game_id=%s", group_id));
    return self.group2banner_[group_id];
end

-- 根据Banner控件id取banner对象
function GroupBannerList:get_banner_by_ctrl(id)
    self:log(string.format("get_banner_by_ctrl, banner_id=%d", id));
    local wrapper_id = MainCommon.get_parent_id(self.uilib_, id);
    return self:get_banner_by_wrapper(wrapper_id);
end

-- 根据Banner控件id取banner对象
function GroupBannerList:get_banner_by_wrapper(id)
    self:log(string.format("get_banner_by_wrapper, wrapper_id=%d", id));
    return self.ctrl2banner_[id];
end

function GroupBannerList:on_tick()
    local lastdate = os.date("*t", self.lasttime_);

    local currenttime = os.time();
    local currentdate = os.date("*t", currenttime);
    self.lasttime_ = currenttime;

    if currentdate.day > lastdate.day then
        self:re_sort_group();
    end
end

function GroupBannerList:set_draggable(index, is_draggable)
    if self.list_ctr_ then
        self.list_ctr_:SetItemDraggable(index, is_draggable);
    end
end
---------------------------------------------------------------------------------------------------
-- 
---------------------------------------------------------------------------------------------------
GroupBannerListEventListener = {
    initialed_ = false;
};

function GroupBannerListEventListener.init()
    if initialed_ then
        return;
    end;
    
end;

function GroupBannerListEventListener.add_tick()
    base_utility.log(string.format("[GroupBannerListEventListener]add_tick"));
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval("GroupBannerListEventListener.on_tick", 30 * 60 * 1000);
end;

function GroupBannerListEventListener.remove_tick()
    base_utility.log(string.format("[GroupBannerListEventListener]remove_tick"));
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval("GroupBannerListEventListener.on_tick");  
end;

function GroupBannerListEventListener.on_tick()
    GroupBannerList:on_tick();
end;

---------------------------------------------------------------------------------------------------
-- 
---------------------------------------------------------------------------------------------------
GroupBannerListUIListener = {
};
